// pages/personal/personal.js
var sex = wx.getStorageSync("sex"); 
var birthday = wx.getStorageSync("birthday");
var date = new Date();
var month = date.getMonth() + 1;
if (month < 10) {
  month = "0" + month;
}
var time = date.getFullYear() + '-' + month + '-' + date.getDate();
const app = getApp();
var router = require("../../router.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xphoto: wx.getStorageSync("head"),
    gender: [    
    ],
    date: "",
    nickname: "",
    introduce: "",  
    sex: "",
    time: time,               
  },
  //上传图片
  xphotos: function () {
    var that = this;
    var xphoto = that.data.xphoto;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {      
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片      
        wx.uploadFile({
          url: router.msg.upload,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData:{
            uploadType: 'img'
          },
          success: function (res) {            
            var result = JSON.parse(res.data);
            if (result.status) {
              that.setData({
                xphoto: result.path
              });
            }
            else {
              wx.showToast({
                title: '图片上传失败请重试',
                content: '图片上传失败请重试',
                icon: 'none'
              })         
            }
          }
        })
      },

    })
  },
  //个人性别
  radioexperience: function (e) {
    var sexVal = 1;
    if (e.detail.value == "女"){
      sexVal = 2      
    }
    this.setData({
      sex: sexVal
    })
  },
  changeInputData: function (e) {
    var that = this;
    var inputId = e.currentTarget.id;
    var inputVlaue = e.detail.value;  
    if (inputId == 'name') {
      that.setData({
        nickname: inputVlaue
      });
    } else {
      that.setData({
        introduce: inputVlaue
      });
    }
  },
  //出生日期选择
  bindDateChange: function (e) {  
    this.setData({
      date: e.detail.value
    })
  },

  subData: function(){
    if (this.data.nickname == ""){
      wx.showToast({
        title: '请填写昵称',       
        icon: 'none'
      })  
      return false;
    }
    var that = this;
    app.request({
      url: router.user.info,
      method: "POST",
      data: {
        uid: wx.getStorageSync('uid'),
        head: that.data.xphoto,
        nickname: that.data.nickname,
        sex: that.data.sex,
        birthday: that.data.date,
        introduce: that.data.introduce,
      },
      success: function (data) {    
        if (data) {            
          wx.setStorageSync("head", that.data.xphoto)
          wx.setStorageSync("nickname", that.data.nickname)
          wx.setStorageSync("sex", that.data.sex)
          wx.setStorageSync("birthday", that.data.date)
          wx.setStorageSync("introduce", that.data.introduce)        
          wx.showToast({
            title: '修改信息成功',
            icon: 'none'
          });
          setTimeout(function () {
            wx.reLaunch({
              url: '../my/my',
            })
          }, 1500);          
        } else {
          wx.showToast({
            title: '修改信息失败,请重试',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      xphoto: wx.getStorageSync("head"),
      gender: [
        { value: "男", checked: sex == 1 ? true : false },
        { value: "女", checked: sex == 2 ? true : false }
      ],
      date: birthday ? birthday : time,
      nickname: wx.getStorageSync("nickname"),
      introduce: wx.getStorageSync("introduce"),
      sex: sex
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      var title = that.data.list[index].name;
      return {
        title: title,
        path: '/pages/personal/personal',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
})